//
//  FQDColumnArticleSmallCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/19/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDColumnArticleSmallCell.h"
#import "FQDMySubscriptionTitleBar.h"
#import "UIButton+FQDExtension.h"
#import "FQDRestModel.h"
#import "FQDArticleFeedCellItem.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface FQDColumnArticleSmallCell ()

@property (nonatomic, strong) FQDMySubscriptionTitleBar *titleBar;
@property (nonatomic, strong) UIView *separatorLine;
@property (nonatomic, strong) UIImageView *articleImageView;
@property (nonatomic, strong) UIButton *commentButton;
@property (nonatomic, strong) UIButton *praiseButton;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation FQDColumnArticleSmallCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}


- (void)prepareUI
{
    _titleBar = [[FQDMySubscriptionTitleBar alloc] init];
    [self.contentView addSubview:_titleBar];
    
    _separatorLine = [[UIView alloc] init];
    _separatorLine.backgroundColor = RGB(244, 244, 244);
    [self.contentView addSubview:_separatorLine];
    
    _articleImageView = [[UIImageView alloc] init];
    _articleImageView.contentMode = UIViewContentModeScaleAspectFill;
    _articleImageView.clipsToBounds = YES;
    [self.contentView addSubview:_articleImageView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FQDBoldFontWithSize(15.0f);
    _titleLabel.textColor = RGB(42, 42, 42);
    _titleLabel.numberOfLines = 3;
    _titleLabel.preferredMaxLayoutWidth = 135.0f;
    [self.contentView addSubview:_titleLabel];
    
    _commentButton = [UIButton commentButtonWithCommentNum:0];
    [self.contentView addSubview:_commentButton];
    
    _praiseButton = [UIButton praiseButtonWithPraiseNum:0];
    [self.contentView addSubview:_praiseButton];
}

- (void)prepareConstraints
{
    WeakSelf;
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
    
    [_titleBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.and.right.equalTo(weakSelf.contentView);
        make.height.equalTo(@40);
    }];
    
    [_separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleBar.mas_bottom);
        make.left.equalTo(weakSelf.contentView).offset(10);
        make.right.equalTo(weakSelf.contentView).offset(-10);
        make.height.equalTo(@0.5);
    }];
    
    [_articleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.separatorLine.mas_bottom).offset(10);
        make.right.equalTo(weakSelf.contentView).offset(-10);
        make.bottom.equalTo(weakSelf.contentView).offset(-10);
        make.width.equalTo(@145);
        make.height.equalTo(@102);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.articleImageView).offset(10);
        make.left.equalTo(weakSelf.contentView).offset(10);
        make.right.equalTo(weakSelf.contentView.mas_centerX).offset(-10);
        make.bottom.lessThanOrEqualTo(weakSelf.commentButton.mas_top).offset(-10);
    }];
    
    [_commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLabel).offset(2);
        make.bottom.equalTo(weakSelf.contentView).offset(-10);
    }];
}

- (void)updateCell
{
    FQDArticleFeedCellItem *cellItem = (FQDArticleFeedCellItem *)self.cellItem;
    FQDPost *post = cellItem.feed.post;
    self.titleBar.post = post;
    
    NSURL *articleImageURL = [NSURL URLWithString:post.image];
    [self.articleImageView sd_setImageWithURL:articleImageURL];
    self.titleLabel.text = post.title;
    
    [self.commentButton setTitle:[@(post.comment_count) stringValue] forState:UIControlStateNormal];
    [self.praiseButton setTitle:[@(post.praise_count) stringValue] forState:UIControlStateNormal];
    self.commentButton.hidden = (post.comment_count > 0) ? NO : YES;
    self.praiseButton.hidden = (post.praise_count > 0) ? NO : YES;
    
    
    WeakSelf;
    [self.praiseButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        if (weakSelf.commentButton.hidden) {
            make.left.equalTo(weakSelf.titleLabel);
        } else {
            make.left.equalTo(weakSelf.commentButton.mas_right).offset(10);
        }
        make.bottom.equalTo(weakSelf.commentButton);
    }];
}

- (void)configureCellWithCellItem:(FQDFeedCellItem *)cellItem
{
    [super configureCellWithCellItem:cellItem];
    
    [self updateCell];
}

+ (NSString *)reuseId
{
    return @"FQDColumnArticleSmallCell";
}

@end
